package examination;

import examination.util.*;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ExamUpdateServlet
 */
@WebServlet("/ExamUpdateServlet")
public class ExamUpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ExamUpdateServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	/**
	 * 更新考试。接受考试所有可修改字段作为参数，调用ExamBean的相关方法对数据库进行修改。
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/**
		 * 获取所有可修改参数
		 * String examNo;//考试编号,该参数不可修改
		 * String status;//考试状态,该参数不可修改
		 * String termNo;//学期号
		 * String college;//学院号
		 * String courseNo;//课程名
		 * String[] classNo;//参加考试的班级号列表
		 * Date date;//考试日期
		 * String time;//考试时间，未格式化文本
		 * String location;//考试地点
		 * int workload;//监考工分(等效于监考工作量)
		 * int need;//监考缺少人数
		 * Date applyDate;//申请考试的日期
		 * String applierNo;//申请考试的职工编号
		 */
		try {
			request.setCharacterEncoding("UTF-8");
			response.setCharacterEncoding("UTF-8");
			String examNo=request.getParameter("examNo");
			String status=request.getParameter("status");
			String courseNo=request.getParameter("courseNo");//课程号
			String[] classNoList=request.getParameterValues("classNo");//参加考试的班级号列表
			String dateString=request.getParameter("date");//考试日期
			String time=request.getParameter("time");//考试时间，未格式化文本
			String location=request.getParameter("location");//考试地点
			String workloadString=request.getParameter("workload");//监考工分(等效于监考工作量)
			String needString=request.getParameter("need");//监考需要人数
			String applyDateString=request.getParameter("applyDate");
			String applierNo=request.getParameter("applierNo");
			String comment = request.getParameter("comment");
			
			
			//若参数不足则跳转回去,comment不是必填项不需要检查
			if(examNo==null||status==null||courseNo==null||
					classNoList==null||dateString==null||time==null||
					workloadString==null||location==null||needString==null||
					applyDateString==null||applierNo==null) {
				response.sendRedirect("exam_info.jsp");
				return;
			}
			SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
			Date date,applyDate = null;
			date = sdf.parse(dateString);
			applyDate = sdf.parse(applyDateString);
			String termNo = ExamBean.getTermNo(date);
			
			int workload=Integer.valueOf(workloadString);
			int need=Integer.valueOf(needString);
			
			ExamBean exam = new ExamBean();
			exam.setExamNo(examNo);
			exam.setTerm(termNo);
			exam.setCourseNo(courseNo);
			exam.setWorkload(workload);
			exam.setDate(date);
			exam.setLocation(location);
			exam.setTime(time);
			exam.setNeed(need);
			exam.setStatus(status);
			exam.setComment(comment);
			exam.setApplierNo(applierNo);
			exam.setApplyDate(applyDate);
			exam.setClassNo(classNoList);
			boolean success = ExamBean.updateExam(exam);
			
			if(success) {
				response.getWriter().write("修改成功！3秒钟跳到考试一览表");
				//设置3秒钟跳转
				response.setHeader("refresh", "3;url=exam_info.jsp");
				return;
			}
			else {
				response.getWriter().write("修改失败！");
				//设置3秒钟跳转
				response.setHeader("refresh", "3;url=exam_info.jsp");
				return;
			}
			
			

		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
